<!--
 * @Description: 
 * @Autor: ZachGmy
 * @Date: 2021-06-21 10:44:09
 * @LastEditors: ZachGmy
 * @LastEditTime: 2021-06-21 11:14:23
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .div1 {
      width: 100px;
      height: 100px;
      background-color: red;
      margin: 0 auto;
    }

    .div2 {
      width: 200px;
      height: 200px;
      background-color: blue;
      /* 绝对定位 */
      /* 父元素内开启相对定位 */
      position: relative;
    }

    .div3 {
      width: 100px;
      height: 100px;
      background-color: yellow;
    }

    .div4 {
      width: 50px;
      height: 50px;
      background-color: aqua;
      /* 子元素内开启绝对定位 */
      position: absolute;
      /* 水平方向上基于父元素居中 */
      left: 50%;
      margin-left: -25px;
    }
  </style>
</head>

<body>
  <div class="div1"></div>
  <div class="div2">
    <div class="div4"></div>
  </div>
  <div class="div3"></div>
</body>

</html>